//! @file   lc_thread_storage_pthread.h
//! @brief  
//! @author Sayan Chaliha
//! @date   September, 2011
//!
//! 
#ifndef _LC_THREAD_STORAGE_H
#error "Include <lc_thread_storage.h> instead!"
#endif /* _LC_THREAD_STORAGE_H */

#ifdef HAVE_PTHREAD
#ifndef _LC_THREAD_STORAGE_PTHREAD_H
#define _LC_THREAD_STORAGE_PTHREAD_H

template <>
class CYAN_API ThreadStorage<void*> {
public:
  typedef void* ValueType;

  ThreadStorage() {
    Int returnCode = ::pthread_key_create(&this->tlsKey_, nullptr);
    if (returnCode != 0)
      System::throwSystemException(returnCode);
  }

  virtual ~ThreadStorage() {
    Int returnCode = ::pthread_key_delete(this->tlsKey_);
    if (returnCode != 0)
      System::throwSystemException(returnCode);
  }

  void* data() const {
    return ::pthread_getspecific(this->tlsKey_);
  }

  void data(ValueType value) {
    Int returnCode = ::pthread_setspecific(this->tlsKey_, value);
    if (returnCode != 0)
      System::throwSystemException(returnCode);
  }

private:
  pthread_key_t tlsKey_;
};

#endif /* _LC_THREAD_STORAGE_PTHREAD_H */
#endif /* HAVE_PTHREAD */
